/*!
 Temelia - Tree performance tests.

 Copyright (C) 2008, 2009 Ceata (http://ceata.org/temelia).

 @author Tibi Turbureanu, Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "include/performance.hpp"
#include "include/testutil.hpp"

extern "C"
{
#include <temelia/tree.h>
#include <temelia/tree_algorithms.h>
}

static volatile int __type;

static tree_t __temelia_tree_insert(int n)
{
	int i, j, k;
	tree_t temelia_tree;

	n /= 1000;
	k = 0;
	temelia_tree = tree_new(default_collection[k++], __type);

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < i + 2 && k < MAX_TEST_SIZE; j++)
			tree_add_child(temelia_tree, tree_new(default_collection[k++],
					__type));
		temelia_tree = tree_get_child(temelia_tree, 0);
		if (k >= MAX_TEST_SIZE)
			break;
	}

	return temelia_tree;
}

static void temelia_tree_insert(int n)
{
	tree_delete(__temelia_tree_insert(n));
}

void TemeliaPerformance::pt_tree(void)
{
	TestUnit testUnit("tree");

	__type = 0;
	GENERATE_TEMELIA_TEST(testUnit, "insert_vector_t", tree_insert);

	__type = 1;
	GENERATE_TEMELIA_TEST(testUnit, "insert_linked_list_t", tree_insert);

	testUnit.write(fout);
}
